#include <reg51.h>
#include <absacc.h>
sbit bp20 = P2 ^ 0;
sbit bp21 = P2 ^ 1;
sbit bp22 = P2 ^ 2;
sbit bp23 = P2 ^ 3;
sbit bp24 = P2 ^ 4;
sbit bp25 = P2 ^ 5;

void InitCom(unsigned char BaudRate)
{
    unsigned char THTL;
    switch (BaudRate)
    {
    case 1:
        THTL = 64;
        break;
    case 2:
        THTL = 160;
        break;
    case 3:
        THTL = 208;
        break;
    case 4:
        THTL = 232;
        break;
    case 5:
        THTL = 244;
        break;
    case 6:
        THTL = 250;
        break;
    case 7:
        THTL = 253;
        break;
    case 8:
        THTL = 255;
        break;
    default:
        THTL = 208;
    }

    SCON = 0x50;
    TMOD = 0x20;
    TCON = 0x40;
    TH1 = THTL;
    TL1 = THTL;
    PCON = 0x80;
    RI = 0;
    TI = 0;
    TR1 = 1;
}

unsigned char ComlnChar()
{
    unsigned char InData;
    while (!RI)
        ;
    InData = SBUF;
    RI = 0;
    return (InData);
}

void hmcode_Decoder(unsigned char xx, unsigned char yy)
{
    unsigned char zxh, nu0, nu1, nu2, nu3, nu4, nu5, nu6, nu7, y, error1, error2, error3, error4, code1, data_in_H, data_in_L, error;
    bit a, b, c, d;
    bit data_in_L4, data_in_L5, data_in_L6, data_in_L7;
    bit data_in_H0, data_in_H1, data_in_H2, data_in_H3, data_in_H4, data_in_H5, data_in_H6, data_in_H7;
    data_in_H = xx;
    y = data_in_H;
    if ((y & 0x80) == 0x80)
    {
        data_in_H7 = 1;
    }
    else
    {
        data_in_H7 = 0;
    }
    if ((y & 0x40) == 0x40)
    {
        data_in_H6 = 1;
    }
    else
    {
        data_in_H6 = 0;
    }
    if ((y & 0x20) == 0x20)
    {
        data_in_H5 = 1;
    }
    else
    {
        data_in_H5 = 0;
    }
    if ((y & 0x10) == 0x10)
    {
        data_in_H4 = 1;
    }
    else
    {
        data_in_H4 = 0;
    }
    if ((y & 0x08) == 0x08)
    {
        data_in_H3 = 1;
    }
    else
    {
        data_in_H3 = 0;
    }
    if ((y & 0x04) == 0x04)
    {
        data_in_H2 = 1;
    }
    else
    {
        data_in_H2 = 0;
    }
    if ((y & 0x02) == 0x02)
    {
        data_in_H1 = 1;
    }
    else
    {
        data_in_H1 = 0;
    }
    if ((y & 0x01) == 0x01)
    {
        data_in_H0 = 1;
    }
    else
    {
        data_in_H0 = 0;
    }
    data_in_L = yy;
    y = data_in_L;
    if ((y & 0x80) == 0x80)
    {
        data_in_L7 = 1;
    }
    else
    {
        data_in_L7 = 0;
    }
    if ((y & 0x40) == 0x40)
    {
        data_in_L6 = 1;
    }
    else
    {
        data_in_L6 = 0;
    }
    if ((y & 0x20) == 0x20)
    {
        data_in_L5 = 1;
    }
    else
    {
        data_in_L5 = 0;
    }
    if ((y & 0x10) == 0x10)
    {
        data_in_L4 = 1;
    }
    else
    {
        data_in_L4 = 0;
    }

    a = data_in_L4 ^ data_in_L6 ^ data_in_H0 ^ data_in_H2 ^ data_in_H6 ^ data_in_H4;
    b = data_in_L5 ^ data_in_L6 ^ data_in_H1 ^ data_in_H2 ^ data_in_H6 ^ data_in_H5;
    c = data_in_L7 ^ data_in_H0 ^ data_in_H1 ^ data_in_H2 ^ data_in_H7;
    d = data_in_H3 ^ data_in_H4 ^ data_in_H5 ^ data_in_H6 ^ data_in_H7;
    error = 0x00;

    if ((a == 1))
        error1 = 0x01;
    else
        error1 = 0x00;

    if ((b == 1))
        error2 = 0x02;
    else
        error2 = 0x00;

    if ((c == 1))
        error3 = 0x04;
    else
        error3 = 0x00;

    if ((d == 1))
        error4 = 0x08;
    else
        error4 = 0x00;

    error = error1 | error2 | error3 | error4;

    if (error == 3)
        data_in_L6 = ~data_in_L6;
    if (error == 5)
        data_in_H0 = ~data_in_H0;
    if (error == 6)
        data_in_H1 = ~data_in_H1;
    if (error == 7)
        data_in_H2 = ~data_in_H2;
    if (error == 9)
        data_in_H4 = ~data_in_H4;
    if (error == 10)
        data_in_H5 = ~data_in_H5;
    if (error == 11)
        data_in_H6 = ~data_in_H6;
    if (error == 12)
        data_in_H7 = ~data_in_H7;

    if (data_in_L6 == 0)
        nu0 = 0x00;
    else
        nu0 = 0x01;
    if (data_in_H0 == 0)
        nu1 = 0x00;
    else
        nu1 = 0x02;
    if (data_in_H1 == 0)
        nu2 = 0x00;
    else
        nu2 = 0x04;
    if (data_in_H2 == 0)
        nu3 = 0x00;
    else
        nu3 = 0x08;
    if (data_in_H4 == 0)
        nu4 = 0x00;
    else
        nu4 = 0x10;
    if (data_in_H5 == 0)
        nu5 = 0x00;
    else
        nu5 = 0x20;
    if (data_in_H6 == 0)
        nu6 = 0x00;
    else
        nu6 = 0x40;
    if (data_in_H7 == 0)
        nu7 = 0x00;
    else
        nu7 = 0x80;

    code1 = nu0 | nu1 | nu2 | nu3 | nu4 | nu5 | nu6 | nu7;
    DBYTE[0x40] = error1;
    zxh = nu4 | nu5 | nu6 | nu7;
    zxh = zxh >> 4;
    DBYTE[0x41] = zxh;
    DBYTE[0x42] = nu0 | nu1 | nu2 | nu3;
}

void delay(unsigned char t)
{
    unsigned char i, j;
    for (i = 0; i < t; i++)
        for (j = 0; j < 200; j++)
            ;
}

void main()
{
    unsigned char x, y, aah, aal, k, i, a[5];
    unsigned char code
    seg7[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
              0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e, 0xff};
    InitCom(4);
    i = 0;
    aah = 0;
    aal = 0;
    while (1)
    {
        a[i] = ComlnChar();
        i++;
        if (i == 3)
        {
            i = 0;
            k = 0;
        }
        else
        {
            k = 0;
        }

        if (k == 1)
        {
            x = a[0] << 4;
        }

        x = x | a[1];
        y = a[2] << 4;
        y = y | 0x00;
        hmcode_Decoder(x, y);
        aah = DBYTE[0x41];
        aal = DBYTE[0x42];
        P0 = seg7[a[0]];
        bp20 = 1;
        delay(10);
        bp20 = 0;
        bp23 = 0;
        P0 = seg7[a[1]];
        bp21 = 1;
        delay(10);
        bp21 = 0;
        P0 = seg7[a[2]];
        bp22 = 1;
        delay(10);
        bp22 = 0;
        P0 = seg7[aah];
        bp24 = 1;
        delay(10);
        bp24 = 0;
        P0 = seg7[aal];
        bp25 = 1;
        delay(10);
        bp25 = 0;
    }
}
